package com.le.buddha.common;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author guole
 * @created 2024/12/19
 */
public class DateUtils {
    private static final long ONE_MINUTE = 60000L;
    private static final long ONE_HOUR = 3600000L;
    private static final long ONE_DAY = 86400000L;
    private static final long ONE_WEEK = 604800000L;

    private static final String ONE_SECOND_AGO = "秒前";
    private static final String ONE_MINUTE_AGO = "分钟前";
    private static final String ONE_HOUR_AGO = "小时前";
    private static final String ONE_DAY_AGO = "天前";
    private static final String ONE_MONTH_AGO = "月前";
    private static final String ONE_YEAR_AGO = "年前";

    public static void main(String[] args) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:m:s");
        Date date = format.parse("2024-11-15 14:00:35");
        System.out.println(format(date));
    }

    public static String format(Date date) {
        Date now=new Date();
        Long minuteNum=DateUtil.between(now,date, DateUnit.MINUTE);
        if(minuteNum<60){
            return (minuteNum <= 0 ? 1 : minuteNum)+ONE_MINUTE_AGO;
        }
        Long hour=DateUtil.between(now,date, DateUnit.HOUR);

        if(hour<24){
            return (hour <= 0 ? 1 : hour)+ONE_HOUR_AGO;
        }
        Long day=DateUtil.between(now,date, DateUnit.DAY);

        if(day<30){
            return (day <= 0 ? 1 : day)+ONE_DAY_AGO;
        }
        Long month=day/30;
        if(month<12){
            return (month <= 0 ? 1 : month)+ONE_MONTH_AGO;
        }
        Long year=day/30/12;
        return (year <= 0 ? 1 : year)+ONE_YEAR_AGO;


    }

    private static long toSeconds(long date) {
        return date / 1000L;
    }

    private static long toMinutes(long date) {
        return toSeconds(date) / 60L;
    }

    private static long toHours(long date) {
        return toMinutes(date) / 60L;
    }

    private static long toDays(long date) {
        return toHours(date) / 24L;
    }

    private static long toMonths(long date) {
        return toDays(date) / 30L;
    }

    private static long toYears(long date) {
        return toMonths(date) / 365L;
    }

}
